package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.app.ArtToGoApp;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.Config;

public class UpdatePasswordActivity extends BaseActivity implements OnClickListener {
	private TextView mUpdateBtn;
	private Intent mIntent;
	private EditText opw,pw,npw;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pass);
		ArtToGoApp.getInstance().addActivity(UpdatePasswordActivity.this);
		initView();
	}

	private void initView() {
		mContext = this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		opw = (EditText) findViewById(R.id.et_opw);
		pw = (EditText) findViewById(R.id.et_pw);
		npw = (EditText) findViewById(R.id.et_npw);
		mBack.setOnClickListener(this);
		mTittle.setText("修改密码");
		mUpdateBtn=(TextView) findViewById(R.id.update_pass_complite);
		mUpdateBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_pass_complite:
			complete();
			
			break;
		case R.id.back_btn:
			finish();
		default:
			break;
		}
	}
	/**
	 * 
	 */
	private void complete() {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(opw.getText().toString())) {
			Toast.makeText(mContext, "旧密码不能为空", 2).show();
			return;
		} else if (TextUtils.isEmpty(pw.getText().toString())) {
			Toast.makeText(mContext, "新密码不能为空", 2).show();
			return;
		} else if (pw.getText().toString().length()<6) {
			Toast.makeText(mContext, "新密码不能少于6位", 2).show();
			return;
		}else if (TextUtils.isEmpty(npw.getText().toString())) {
			Toast.makeText(mContext, "请再次输入密码", 2).show();
			return;
		} else if (!pw.getText().toString().equals(npw.getText().toString())) {
			Toast.makeText(mContext, "两次输入密码不一致", 2).show();
			return;
		}
	VolleyBasic.getInstance(UpdatePasswordActivity.this).post(Cache.modifypwd, VolleyMap.getInstance().modifyPwd( opw.getText().toString()
			, pw.getText().toString(),  AppUtil.getUser().getToken()), AppItem.class, new VolleyCallback() {
				
				@Override
				public void onSuccess(VolleyData vd) {
					// TODO Auto-generated method stub
					Config.showTip(mContext, vd.getMsg());
					mIntent = new Intent(UpdatePasswordActivity.this, MainActivity.class);
					startActivity(mIntent);
					ArtToGoApp.getInstance().exit();
				}
				
				@Override
				public void onError(VolleyError volleyError) {
					// TODO Auto-generated method stub
					
				}
			});
	}
	
	
}
